home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 9 / HOWMANY2.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  3KB  |  87 lines

  1. // File from page 379 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: HOWMANY2.CPP -- The copy-constructor
  34. #include <fstream.h>
  35. #include <string.h>
  36. ofstream out("howmany2.out");
  37.  
  38. class howmany2 {
  39.   enum { bufsize = 30 };
  40.   char id[bufsize]; // Object identifier
  41.   static int object_count;
  42. public:
  43.   howmany2(const char* ID = 0) {
  44.     if(ID) strncpy(id, ID, bufsize);
  45.     else *id = 0;
  46.     ++object_count;
  47.     print("howmany2()");
  48.   }
  49.   // The copy-constructor:
  50.   howmany2(const howmany2& h) {
  51.     strncpy(id, h.id, bufsize);
  52.     strncat(id, " copy", bufsize - strlen(id));
  53.     ++object_count;
  54.     print("howmany2(howmany2&)");
  55.   }
  56.   // Can't be static (printing id):
  57.   void print(const char* msg = 0) const {
  58.     if(msg) out << msg << endl;
  59.     out << '\t' << id << ": "
  60.         << "object_count = "
  61.         << object_count << endl;
  62.   }
  63.   ~howmany2() {
  64.     --object_count;
  65.     print("~howmany2()");
  66.   }
  67. };
  68.  
  69. int howmany2::object_count = 0;
  70.  
  71. // Pass and return BY VALUE:
  72. howmany2 f(howmany2 x) {
  73.   x.print("x argument inside f()");
  74.   out << "returning from f()" << endl;
  75.   return x;
  76. }
  77.  
  78. main() {
  79.   howmany2 h("h");
  80.   out << "entering f()" << endl;
  81.   howmany2 h2 = f(h);
  82.   h2.print("h2 after call to f()");
  83.   out << "call f(), no return value" << endl;
  84.   f(h);
  85.   out << "after call to f()" << endl;
  86. }
  87.